﻿
$(document).ready(function () {

    var jsonEnterprise = {
        enterpriseId: $('form').attr("id"),
    };

    getEconomicGroupList();
    getSectorList();
    getEnterprise(jsonEnterprise);
    
    

    $('#btnSave').click(function () {

        jsonEnterprise = {
            enterpriseId: $('form').attr("id"),
            ruc: $('#txtRUC').val(),
            socialReason: $('#txtSocialReason').val(),
            sectorId:$('#optSector').val(),
            cem: $('#txtRisk').val(),
            creationDate: $('#txtDate').val(),
            groupId :  $('#optEconomicGroup').val(),
           defaultState: $('#txtValue').val(),
          agentName: $('#txtName').val(),
         agentFirstName: $('#txtFirstName').val(),
         agentLastName : $('#txtLastName').val(),
         officialAddress: $('#txtAddress').val(),
         agentDNI :$('#txtDNI').val(),
         agentPhone: $('#txtPhone').val(),
         agentEmail: $('#txtEmail').val(),


        };

        editEnterprise(jsonEnterprise);
    });



});






function getSectorList() {

    $.ajax({
        type: "POST",
        data: JSON.stringify(),
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        url: "getSectorList",
   
        success: function (data) {

            var result = "";

            $.each(data, function (i, item) {

                result += '<option value="'+ item.sectorId + '">'+ item.name + '</option>';

            });

            $('#optSector').html(result);

           

        }

    });//Fin del Ajax

}

function getEnterprise(jsonEnterprise) {
    $.ajax({
        type: "POST",
        data: JSON.stringify(jsonEnterprise),
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        url: "getEnterprise",
       
       
        success: function (data) {

            $('#txtRUC').val(data.ruc);
            $('#txtSocialReason').val(data.socialReason);
            $('#optSector').val(data.sectorId);
            $('#txtRisk').val(data.cem);
            $('#txtDate').val(data.creationDateAux);
            $('#optEconomicGroup').val(data.groupId);
            $('#txtValue').val(data.defaultState);
            $('#txtName').val(data.agentName);
            $('#txtFirstName').val(data.agentFirstName);
            $('#txtLastName').val(data.agentLastName);
            $('#txtAddress').val(data.officialAddress);
            $('#txtDNI').val(data.agentDNI);
            $('#txtPhone').val(data.agentPhone);
            $('#txtEmail').val(data.agentEmail);


        }

    });//Fin del Ajax
}

function getEconomicGroupList() {

    $.ajax({
        type: "POST",
        data: JSON.stringify(),
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        url: "getEconomicGroupList",

        success: function (data) {

            var result = "";

            $.each(data, function (i, item) {

                result += '<option value="' + item.economicGroupId + '">' + item.name + '</option>';

            });

            $('#optEconomicGroup').html(result);


        }

    });//Fin del Ajax
}

function editEnterprise(jsonEnterprise) {
        $.ajax({
            type: "POST",
            data: JSON.stringify(jsonEnterprise),
            dataType: "json",
            contentType: "application/json; charset=utf-8",
            url: "editEnterprise",
            success: function (data) {

            }

        });//Fin del Ajax
    }